package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.blb.entity.*;
import com.blb.service.*;
import com.blb.utils.DataTablesPage;
import com.blb.utils.ShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *采购管理在线下单和采购列表
 * @author tb
 * @since 2020-05-20
 */
@Controller
@RequestMapping("/procurement")
public class ProcurementController {

    @Autowired
    private IProcurementOrderService procurementOrderService;
    @Autowired
    private IProductService productService;

    /**
     * 采购列表
     */
    @RequiresPermissions("purchaselist")
    @RequestMapping("list")
    @ResponseBody
    public DataTablesPage procurementOrder(Integer draw,Integer start, Integer length,String pname,String status) {
        int count = procurementOrderService.getProcurementOrderCount(pname,status,null,ShiroUtils.getCompany());
        List<ProcurementOrder> list = procurementOrderService.getProcurementOrder(null, start, length,pname,status,null,ShiroUtils.getCompany());
        return new DataTablesPage(draw,count,count, list);
    }

    /**
     * 最近订单
     */
    @RequiresPermissions("purchaselist")
    @RequestMapping("latelyOrder")
    @ResponseBody
    public DataTablesPage procurementOrderLately(Integer draw,Integer start, Integer length,String pname,String status) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        int count = procurementOrderService.getProcurementOrderCount(pname,status,true,user.getCompany().getId());
        List<ProcurementOrder> list = procurementOrderService.getProcurementOrder(null, start, length,pname,status,true,user.getCompany().getId());
        return new DataTablesPage(draw,count,count, list);
    }

    /**
     * 订单详情
     */
    @RequiresPermissions("purchaselist:select")
    @RequestMapping("details")
    public String procurementDetails(String id, Model model) {
        List<ProcurementOrder> pOrder = procurementOrderService.getProcurementOrder(id, null, null,null,null,null,null);
        model.addAttribute("pOrder", pOrder.get(0));
        return "procurement/list/od_purchasing";
    }

    /**
     * 查看物流
     */
    @RequiresPermissions("purchaselist")
    @RequestMapping("logistics")
    public String procurementLogistics(String id, Model model) {
        List<ProcurementOrder> pOrder = procurementOrderService.getProcurementOrder(id, null, null,null,null,null,null);
        model.addAttribute("pOrder", pOrder.get(0));
        return "procurement/list/order_wl";
    }

    /**
     * 确认收货
     */
    @RequiresPermissions("purchaselist")
    @RequestMapping("take")
    @ResponseBody
    public String takeLogistics(String orderId) {
        Order o = new Order();
        o.setOrderStatus("已签收");

        OrderLog orderLog = new OrderLog();
        orderLog.setOrderId(orderId);
        orderLog.setOrderTime(new Date());
        orderLog.setProduction("订单已确认");

        if (procurementOrderService.takeProduct(o,orderId,orderLog)) {
            return "{\"result\":true}";
        } else {
            return "{\"result\":false}";
        }
    }

    /**
     * 在线下单列表
     */
    @RequiresPermissions("online")
    @ResponseBody
    @RequestMapping("orderProduct")
    public DataTablesPage orderProduct(Integer draw,Integer start, Integer length,String sid,Integer sortType) {

        List<ProcurementOrder> list = procurementOrderService.orderProduct(null,start,length,sid,sortType,ShiroUtils.getCompany());
        int count = procurementOrderService.orderProductCount(sid,sortType,ShiroUtils.getCompany());
        return new DataTablesPage(draw,count,count, list);
    }

    /**
     * 商品分类树形
     * @return
     */
    @RequiresPermissions("online")
    @ResponseBody
    @RequestMapping("sortTree")
    public List<Sort> sortTree() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        System.out.println(user.getCompany().getId() + 1);
        return procurementOrderService.sortTree(user.getCompany().getId());
    }

    /**
     * 未下单商品详细信息
     */
    @RequiresPermissions("online")
    @RequestMapping("orderDetails")
    public String orderDetails(String no, Model model) {
        List<ProcurementOrder> list = procurementOrderService.orderProduct(no,null,null,null,null,null);
        model.addAttribute("order", list.get(0));
        return "procurement/order/online_order_xd_detail";
    }

    /**
     * 购买商品显示商品信息
     */
    @RequiresPermissions("onlinebuy:select")
    @RequestMapping("buy_product")
    public String buy_product(String no, Model model) {
        List<ProcurementOrder> list = procurementOrderService.orderProduct(no,null,null,null,null,null);
        model.addAttribute("product", list.get(0));
       return "procurement/order/buy_product";
    }

    /**
            * 多件购买显示商品信息
     */
    @RequiresPermissions("online")
    @RequestMapping("buys_product")
    public String buys_product(String[] nos, Model model) {
        List<ProcurementOrder> products = new ArrayList<>();
        for (String no : nos){
            List<ProcurementOrder> list = procurementOrderService.orderProduct(no,null,null,null,null,null);
            products.add(list.get(0));
        }
        model.addAttribute("products", products);
        return "procurement/order/buy_products";
    }

    /**
     * 确认购买
     */
    @RequiresPermissions("online:buy")
    @RequestMapping("buy")
    @ResponseBody
    public String buy(String no) {
        Order o = new Order();
        o.setOrderStatus("未排单");
        o.setPayment("已付款");
        Date date = new Date();
        o.setPayTime(date);

        OrderLog orderLog = new OrderLog();
        orderLog.setOrderId(no);
        orderLog.setOrderTime(date);
        orderLog.setProduction("下单成功");

        if (procurementOrderService.buyProduct(o,no,orderLog)) {
            return "{\"result\":true}";
        } else {
            return "{\"result\":false}";
        }
    }

    /**
     * 多件购买
     */
    @RequiresPermissions("online")
    @RequestMapping("buys")
    @ResponseBody
    public String buys(String[] nos) {
        Order o = new Order();
        o.setOrderStatus("未排单");
        o.setPayment("已付款");
        Date date = new Date();
        o.setPayTime(date);

        List<OrderLog> list = new ArrayList<>();
        for (String no : nos){
            OrderLog orderLog = new OrderLog();
            orderLog.setOrderId(no);
            orderLog.setOrderTime(date);
            orderLog.setProduction("下单成功");
            list.add(orderLog);
        }

        if (procurementOrderService.buyProducts(o,nos,list,date)) {
            return "{\"result\":true}";
        } else {
            return "{\"result\":false}";
        }
    }
    @RequiresPermissions("online")
    @ResponseBody
    @RequestMapping("productType")
    public Map<String,String> productType(String id){
        Product product = productService.getOne(new QueryWrapper<Product>().eq("id", id));
        String productType = product.getProductType();
        Map<String,String> map = new HashMap<>();
        map.put("type",productType);
        return map;
    }
}

